package com.hhh.sun.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hhh.sun.pojo.entity.TbUser;
import com.hhh.sun.service.UserService;
import com.hhh.sun.vo.req.AddUserVo;
import com.hhh.sun.vo.req.LoginReqVo;
import com.hhh.sun.vo.resp.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 用户操作接口
 */
@Api(value = "/sun", tags = {"用户操作接口"})
@RequestMapping("/sun")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "LoginReqVo", name = "vo", value = "", required = true)
    })
    @ApiOperation(value = "用户登录", notes = "", httpMethod = "POST")
    @PostMapping("/user/login")
    public R<LoginRespVo> login(@RequestBody LoginReqVo vo){
        return userService.login(vo);
    }
    /**
     * 获取用户的详细信息
     * @param userName 用户名字
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "userName", value = "用户名字", required = true)
    })
    @ApiOperation(value = "根据用户名查询用户的详细信息", notes = "获取用户的详细信息", httpMethod = "GET")
    @GetMapping("/user/{userName}")
    public R<QueryUserVo>getUserInfo(@PathVariable("userName")String userName){
        TbUser user = userService.getOne(Wrappers.<TbUser>lambdaQuery().eq(TbUser::getUserName, userName));
        if(user==null){
            return R.error(ResponseCode.NO_RESPONSE_DATA);
        }
        QueryUserVo vo = new QueryUserVo();
        BeanUtils.copyProperties(user,vo);
        return R.ok(vo);
    }
    /**
     * 添加用户
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "AddUserVo", name = "vo", value = "", required = true)
    })
    @ApiOperation(value = "添加用户", notes = "添加用户", httpMethod = "POST")
    @PostMapping("/user")
    public R<Object>addUser(@RequestBody AddUserVo vo){
        TbUser user = userService.getOne(Wrappers.<TbUser>lambdaQuery().eq(TbUser::getUserName, vo.getUserName()));
        if(user!=null){
            return R.error(ResponseCode.ERROR);
        }
        TbUser tbUser = new TbUser();
        BeanUtils.copyProperties(vo,tbUser);
        boolean b = userService.save(tbUser);
        return b?R.ok(ResponseCode.SUCCESS):R.error(ResponseCode.ERROR);
    }
    /**
     * 分页查询所有用户
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "pageNum", value = "当前页数"),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "pageSize", value = "每页大小")
    })
    @ApiOperation(value = "分页查询所有用户", notes = "分页查询所有用户", httpMethod = "POST")
    @PostMapping("/users")
    public R<PageResult<TbUser,QueryUserVo>>findAllUser(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,@RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize){
        return userService.findAllUser(pageNum,pageSize);
    }

    /**
     * 根据用户名字删除用户信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "userName", value = "", required = true)
    })
    @ApiOperation(value = "根据用户名字删除用户信息", notes = "根据用户名字删除用户信息", httpMethod = "DELETE")
    @DeleteMapping("/user/{userName}")
    public R<Object>deleteUserByName(@PathVariable("userName")String userName){
        boolean b = userService.remove(Wrappers.<TbUser>lambdaQuery().eq(TbUser::getUserName, userName));
        return b?R.ok(ResponseCode.SUCCESS):R.error(ResponseCode.ERROR);
    }

    /**
     * 操作某个用户是否加入黑名单，1为加入黑名单，0为拉出黑名单
     * @param userName 用户名字
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "userName", value = "用户名字", required = true),
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "op", value = "", required = true)
    })
    @ApiOperation(value = "操作某个用户是否加入黑名单，1为加入黑名单，0为拉出黑名单", notes = "操作某个用户是否加入黑名单，1为加入黑名单，0为拉出黑名单", httpMethod = "PUT")
    @PutMapping("/user/{userName}/{op}")
    public R<Object>banUser(@PathVariable("userName")String userName,@PathVariable("op")Integer op){
        boolean b;
        if(op==1) {
            b = userService.update(Wrappers.<TbUser>lambdaUpdate().eq(TbUser::getUserName, userName).set(TbUser::getIsban, 1));
        }else {
            b = userService.update(Wrappers.<TbUser>lambdaUpdate().eq(TbUser::getUserName, userName).set(TbUser::getIsban, 0));
        }
        return b?R.ok(ResponseCode.SUCCESS):R.error(ResponseCode.ERROR);
    }


}
